var _util = require('util/util.js');
require('page/common/top/index.js');
require('./index.css');
var listTemplate = require('./index.string');

var _account_service = require('service/account-service.js')

var userLogin = {

    listDate : {
        way      : '',
        signOn : '',
        signOnByCode   : '',
    },

    init : function(){
        this.loadLogin();
        $('#loginMsg').hide();
        this.bindEvents();

        return this;
    },
    bindEvents : function(){
        var _this = this;
        $('#submit').click(
            function(){
                _this.submit();
            }
        );

        $('#submitByCode').click(
            function(){
                _this.submitByCode();
            }
        );

        $('#sendBtn').click(
            function(){
                _this.sendCode();
            }
        );
        
    },

    loadLogin:function(){
        var route = _util.getURLParam('route');
        var _this = this;

        if(route === 'loginByCode'){

            //添加头部信息
            _this.listDate.signOnByCode=true;
            _this.listDate.signOn=false;
            //渲染数据
            var result = _util.renderHtml(listTemplate,{ListDate : _this.listDate})
            console.log(result);
            $('#Catalog').html(result);

        }

    },

    submit : function(){
        var formData = {
            username : $.trim($('#username').val()),
            password : $.trim($('#password').val()),
        };
        var validationResult = this.formValidation(formData);
        if(validationResult.status){
            //向服务器发送请求
            _account_service.login(
                formData,
                function(res){
                    window.location.href = _util.getURLParam('redirect') || './catalog-main.html';
                    // _util.getURLParam('redirect');
                },
                function(errMsg){
                    $('#loginMsg').show().text(errMsg);    
                }
            )
        }
        else{
            $('#loginMsg').show().text(validationResult.msg);     
        }
    },

    submitByCode : function(){
        var formData = {
            memPhone : $.trim($('#memPhone').val()),
            code : $.trim($('#code').val()),
        };
        var validationResult = this.formValidationCode(formData);
        if(validationResult.status){
            //向服务器发送请求
            _account_service.loginByCode(
                formData,
                function(res){
                    window.location.href = _util.getURLParam('redirect') || './catalog-main.html';
                    // _util.getURLParam('redirect');
                },
                function(errMsg){
                    $('#loginMsg').show().text(errMsg);    
                }
            )
        }
        else{
            $('#loginMsg').show().text(validationResult.msg);     
        }
    },


    //发送验证码
    sendCode:function () {
        var memPhone = $("#memPhone").val();

        var formData = {
            memPhone : $.trim($('#memPhone').val()),
            code : $.trim($('#code').val()),
        };
        var validationResult = this.formValidationSendCode(formData);
        if(!validationResult.status){
            $('#loginMsg').show().text(validationResult.msg);  
            return;
        }

        if (memPhone == '' || memPhone.length != 11) {
/*             alert("请输入正确的手机号！"); */
            return;
        } else {
            $.ajax({
                type: 'get',
                url:_util.getServerURL('signon/sendCode'),
                data: {
                    memPhone: memPhone,
                },
                dataType: 'json',
                success: function (data) {
                    if (true) {
                        timer();
                    } else {
                        alert("获取验证码失败");
                    }
                },
                error: function (data) {
                    alert('连接超时！');
                },
            });
        }

    var wait = 60;

    //倒计时
    function timer() {
        if (wait == 0) {
            $("#sendBtn").val("获取验证码");
            $("#sendBtn").removeAttr("disabled");
            $("#sendBtn").css("border-color", "1e9fff").css("background", "#ffffff").css("cursor", "pointer");
            wait = 60;
        } else {
            $("#sendBtn").attr("disabled", "true");
            // $("#sendBtn").css("border-color", "fbfbfb").css("background", "#ccc").css("cursor", "not-allowed");

            $("#sendBtn").text(wait + "秒后重发");
            wait--;
            setTimeout(function () {
                timer()
            }, 1000);
        }
      }
    },

    
    formValidation : function(formData){
        var result = {
            status : false,
            msg    : '',
        }
        if(!_util.validateField('require',formData.username)){
            result.msg = '用户名不能为空';
            return result;
        }
        if(!_util.validateField('require',formData.password)){
            result.msg = '密码不能为空';
            return result;
        }
        //校验通过
        result.status =  'true';
        result.msg    =  '校验通过';
        return result;
    },

    formValidationSendCode : function(formData){
        var result = {
            status : false,
            msg    : '',
        }
        if(!_util.validateField('require',formData.memPhone)){
            result.msg = '手机号不能为空';
            return result;
        }

        if(!_util.validateField('phoneNumber',formData.memPhone)){
            result.msg = '手机号输入错误';
            return result;
        }

        //校验通过
        result.status =  'true';
        result.msg    =  '校验通过';
        return result;
    },

    formValidationCode : function(formData){
        var result = {
            status : false,
            msg    : '',
        }
        if(!_util.validateField('require',formData.memPhone)){
            result.msg = '手机号不能为空';
            return result;
        }

        if(!_util.validateField('phoneNumber',formData.memPhone)){
            console.log(memPhone),
            result.msg = '手机号输入错误';
            return result;
        }

        if(!_util.validateField('require',formData.code)){
            result.msg = '验证码不能为空';
            return result;
        }
        //校验通过
        result.status =  'true';
        result.msg    =  '校验通过';
        return result;
    }

    
};

module.exports = userLogin.init();